<%@ page import="com.dsj.entity.OrderEntity" %>
<%@ page import="com.dsj.entity.OrderItemEntity" %>
<%@ page import="com.dsj.dao.OrderDao" %>
<%@ page import="com.dsj.dao.OrderItemDao" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.util.Vector" %>
<%@ page import="com.dsj.cart.CartGoods" %>
<%@ page import="com.dsj.dao.ProductDao" %><%--
  订单数据写入
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    request.setCharacterEncoding("UTF-8");
    OrderEntity orderEntity = new OrderEntity();
    OrderItemEntity orderItemEntity = new OrderItemEntity();
    OrderDao orderDao = new OrderDao();
    OrderItemDao orderItemDao = new OrderItemDao();

    ProductDao dao = new ProductDao();

    //获取订单id
    String orderId = session.getAttribute("orderId").toString();
    //订单数据准备
    orderEntity.setOrderId(orderId);
    orderEntity.setName(request.getParameter("name"));
    orderEntity.setRealName(request.getParameter("realName"));
    orderEntity.setAddress(request.getParameter("address"));
    orderEntity.setMobile(request.getParameter("mobile"));
    orderEntity.setTotalPrice(Float.parseFloat(request.getParameter("totalPrice")));
    orderEntity.setPaymentMode(request.getParameter("paymentMode"));
    orderEntity.setDeliveryMethod(request.getParameter("deliveryMethod"));
    orderEntity.setMemo(request.getParameter("memo"));
    orderEntity.setDeliverySign(false);
    Date date = new Date();
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    orderEntity.setCreateTime(simpleDateFormat.format(date));
    //订单数据写入
    orderDao.insertOrder(orderEntity);

    //订单明细处理
    Vector cart = (Vector) session.getAttribute("__CART__");
    for (int i = 0; i < cart.size(); i++) {
        //订单明细数据准备
        CartGoods cartGoods = (CartGoods) cart.elementAt(i);
        orderItemEntity.setOrderId(orderId);
        orderItemEntity.setProductId(cartGoods.id);
        orderItemEntity.setProductName(dao.selectOneNameByProductId(cartGoods.id));
        orderItemEntity.setProductPrice(cartGoods.price);
        orderItemEntity.setAmount(cartGoods.number);
        //修改商品的销售数量
        dao.updateProductAmount(cartGoods.id, cartGoods.number);
        //写入订单时明细
        orderItemDao.insertOrderItem(orderItemEntity);
    }

    //删除购物车
    session.removeAttribute("__CART__");

%>
<html>
<head>
    <title>Title</title>
</head>
<body>
<div style="margin: 0 auto; width: 1024px;">
    <h2>订单提交成功</h2>
    <h2>订单编号是：<%=orderId%></h2>
    <h2>请您在24小时内完成支付</h2>
    <h2>感谢您的购买，欢迎下次光临</h2>
    <h2><a href="../product/main.jsp">返回首页</a></h2>
</div>
</body>
</html>
